<?php

use yii\db\Migration;

/**
 * Class m190305_034015_project
 */
class m190305_034015_project extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable('{{%node}}', [
            'id' => $this->primaryKey(),
            'name' => $this->string()->comment('节点名称')->notNull()->unique(),
            'sort' => $this->tinyInteger(3)->comment('排序')->defaultValue(0),
            'parent_id' => $this->integer()->defaultValue(0)->comment('父级节点ID'),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer(),
        ],$tableOptions);

        $this->createTable('{{%project}}', [
            'id' => $this->primaryKey(),
            'name' => $this->string(200)->comment('客户姓名'),
            'sex' => $this->tinyInteger(2)->defaultValue(3)->comment('客户性别，【1：男，2：女，3：保密】'),
            'phone' => $this->string(20)->comment('联系电话'),
            'other_phone' => $this->string(40)->comment('其它电话'),
            'company' => $this->string(100)->comment('公司名称'),
            'node_id' => $this->tinyInteger(3)->defaultValue(0)->comment('节点ID'),
            'follow_at' => $this->integer()->defaultValue(0)->comment('最近跟进时间'),
            'follow_user_id' => $this->tinyInteger(3)->defaultValue(0)->comment('跟进用户ID'),
            'next_follow_at' => $this->integer()->defaultValue(0)->comment('下次跟进时间'),
            'created_user_id' => $this->tinyInteger(3)->defaultValue(0)->comment('创建用户ID'),
            'updated_user_id' => $this->tinyInteger(3)->defaultValue(0)->comment('更新用户ID'),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer(),
            'deleted_at' => $this->integer()->defaultValue(0),
            'deleted_user_id' => $this->tinyInteger()->defaultValue(0),
        ],$tableOptions);

        $this->createTable('{{%project_remark}}', [
            'id' => $this->primaryKey(),
            'project_id' => $this->integer()->comment('所属项目ID'),
            'content' => $this->text()->comment('备注内容'),
            'next_follow_at' => $this->integer()->comment('下次跟进时间'),
            'user_id' => $this->integer()->comment('跟进用户ID'),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer(),
        ],$tableOptions);

        $this->createTable('{{%project_node}}', [
            'id' => $this->primaryKey(),
            'project_id' => $this->integer()->comment('所属项目ID'),
            'old' => $this->integer()->comment('原节点值'),
            'new' => $this->integer()->comment('现节点值'),
            'content' => $this->text()->comment('内容'),
            'user_id' => $this->integer()->comment('操作用户ID'),
            'created_at' => $this->integer(),
            'updated_at' => $this->integer(),
        ],$tableOptions);

        $this->addForeignKey('project-node','project_node','project_id','project','id','CASCADE');
        $this->addForeignKey('project-remark','project_remark','project_id','project','id','CASCADE');

    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable('{{%project_remark}}');
        $this->dropTable('{{%project_node}}');
        $this->dropTable('{{%project}}');
        $this->dropTable('{{%node}}');
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m190305_034015_project cannot be reverted.\n";

        return false;
    }
    */
}
